package com.jishuo.nontax.bfc.service;

import com.jishuo.base.bean.BaseResult;
import com.jishuo.base.bean.ErrorCodeEnum;
import com.jishuo.nontax.bean.message.SetUnitVsItem;
import com.jishuo.nontax.bean.message.SetUnitVsItemDTO;
import com.jishuo.nontax.bfc.dao.UnitVsItemMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import java.io.StringWriter;
import java.util.ArrayList;

/**
 * @auther dinggaofei
 * @Date 2022/12/30 9:58
 */
@Service
public class UnitVsItemService {


    private static Logger logger = LoggerFactory.getLogger(UnitVsItemService.class);
    @Resource
    private UnitVsItemMapper unitVsItemMapper;

    /**
     * @param
     * @auther dinggaofei
     * @Date 2022/12/30 9:33
     * @description 根据单位唯一标识查询单位标识, 项目标识, 编号
     */

    public BaseResult selUnitVsItem(int unitid) {

        try {
            ArrayList<SetUnitVsItemDTO> list = unitVsItemMapper.selUnitVsItem(unitid);
            //查询返回记录
            if (list != null && list.size() > 0) {
                //xml解析器
                Serializer serializer = new Persister();
                //xml转换器
                StringWriter sw = new StringWriter();
                SetUnitVsItem xml = new SetUnitVsItem();
                xml.setUnitvsitem(list);

                serializer.write(xml, sw);
                return new BaseResult(sw.toString());
            } else {
                return new BaseResult("<bills></bills>");
            }
        } catch (Exception e) {
            logger.info("失败：" + e.toString());
            return new BaseResult(ErrorCodeEnum.UNKNOWN_ERR, "<bills></bills>");
        }
    }
}
